package com.czollmann.android.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.czollmann.android.Constants;
import com.czollmann.android.R;

public class SettingsLimitActivity extends SettingsBaseActivity {
  private int limitValue = 0;
  private RadioButton radioButtonLimitEntrys = null;
  private RadioButton radioButtonLimitDays = null;
  private SeekBar seekBarLimit = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_limit);
    final SharedPreferences settings = getSharedPreferences(Constants.SETTINGS_NAME, 0);
    /**
     * Radio Buttons Limit
     */
    radioButtonLimitEntrys = (RadioButton) findViewById(R.id.setting_limit_entrys);
    radioButtonLimitDays = (RadioButton) findViewById(R.id.setting_limit_days);
    OnClickListener onClickListener = new OnClickListener() {
      @Override
      public void onClick(View arg0) {
        SharedPreferences.Editor editor = settings.edit();
        if (radioButtonLimitEntrys.isChecked()) {
          limitValue = Constants.SETTINGS_LIMIT_MAX_ENTRYS;
          editor.putBoolean(Constants.SETTINGS_LIMIT_ENTRYS, true);
          editor.putBoolean(Constants.SETTINGS_LIMIT_DAYS, false);
        } else {
          limitValue = Constants.SETTINGS_LIMIT_MAX_DAYS;
          editor.putBoolean(Constants.SETTINGS_LIMIT_ENTRYS, false);
          editor.putBoolean(Constants.SETTINGS_LIMIT_DAYS, true);
        }
        editor.putInt(Constants.SETTINGS_LIMIT_COUNT, limitValue);
        editor.commit();
        hasChanged = true;
        int seekbarValue = getCorrectSeekbarValue();
        seekBarLimit.setProgress(seekbarValue);
        setSeektext(limitValue);
      }
    };
    radioButtonLimitEntrys.setOnClickListener(onClickListener);
    radioButtonLimitDays.setOnClickListener(onClickListener);
    if (settings.getBoolean(Constants.SETTINGS_LIMIT_ENTRYS, true)) {
      radioButtonLimitEntrys.setChecked(true);
    } else {
      radioButtonLimitDays.setChecked(true);
    }
    int settingsLimitValue = settings.getInt(Constants.SETTINGS_LIMIT_COUNT, 0);
    if (settingsLimitValue != 0) {
      limitValue = settingsLimitValue;
    } else {
      if (radioButtonLimitEntrys.isChecked()) {
        limitValue = Constants.SETTINGS_LIMIT_MAX_ENTRYS;
      } else {
        limitValue = Constants.SETTINGS_LIMIT_MAX_DAYS;
      }
    }
    int seekbarValue = getCorrectSeekbarValue();
    /**
     * Seek Bar Limit
     */
    seekBarLimit = (SeekBar) findViewById(R.id.setting_limit_seekbar);
    seekBarLimit.setProgress(seekbarValue);
    setSeektext(limitValue);
    seekBarLimit.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(Constants.SETTINGS_LIMIT_COUNT, limitValue);
        editor.commit();
        hasChanged = true;
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
      }

      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        int progressSpecified = getCorrectProgressValue(progress);
        limitValue = progressSpecified;
        setSeektext(progressSpecified);
      }
    });
  }

  protected void setSeektext(int progressSpecified) {
    TextView seekTextView = (TextView) findViewById(R.id.setting_limit_seekbar_progress);
    String title = getString(R.string.settings_limitby_days_value);
    if (radioButtonLimitEntrys.isChecked()) {
      title = getString(R.string.settings_limitby_entrys_value);
    }
    seekTextView.setText(title + progressSpecified);
  }

  private int getCorrectProgressValue(int progress) {
    double valuePerOne = 0;
    if (radioButtonLimitEntrys.isChecked()) {
      valuePerOne = Constants.SETTINGS_LIMIT_MAX_ENTRYS * 0.01;
    } else {
      valuePerOne = Constants.SETTINGS_LIMIT_MAX_DAYS * 0.01;
    }
    return (int) (progress * valuePerOne);
  }

  private int getCorrectSeekbarValue() {
    if (radioButtonLimitEntrys.isChecked()) {
      return (int) (((double) limitValue / Constants.SETTINGS_LIMIT_MAX_ENTRYS) * 100);
    } else {
      return (int) (((double) limitValue / Constants.SETTINGS_LIMIT_MAX_DAYS) * 100);
    }
  }
}
